﻿using RestSharp;

namespace Digitalmes.Infrastructure.Common.Http;

/// <summary>
/// HTTP REST 客户端。
/// </summary>
internal sealed class RestGalleryClient : IGalleryClient
{
    private readonly RestClient _client;

    public RestGalleryClient(string baseUrl)
    {
        _client = new RestClient(new RestClientOptions(baseUrl), useClientFactory: true);
    }

    public Task<TResponse?> GetJsonAsync<TResponse>(string resource, CancellationToken cancellationToken = default)
    {
        return _client.GetJsonAsync<TResponse>(resource, cancellationToken);
    }

    public Task<TResponse?> PostJsonAsync<TRequest, TResponse>(string resource, TRequest request, CancellationToken cancellationToken = default)
        where TRequest : class
    {
        return _client.PostJsonAsync<TRequest, TResponse>(resource, request, cancellationToken);
    }

    public Task<TResponse?> PutJsonAsync<TRequest, TResponse>(string resource, TRequest request, CancellationToken cancellationToken = default)
       where TRequest : class
    {
        return _client.PutJsonAsync<TRequest, TResponse>(resource, request, cancellationToken);
    }

    public void Dispose()
    {
        _client.Dispose();
    }
}
